<?php

namespace App\Http\Requests;

use App\Models\ProductSku;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class OrderRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return auth()->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'address_id'    =>[
                'required' ,
                Rule::exists( 'user_addresses' , 'id' )->where( 'user_id' , $this->user()->id ) ,
            ] ,
            'items'         =>[ 'required' , 'array' ] ,
            'items.*.sku_id'=>[// 检查 items 数组下每一个子数组的 sku_id 参数
                function( $attribute , $value , $fail )
                {
                    if( !$sku=ProductSku::find( $value ) ){
                        $fail( '商品不存在' );
                    }
                    if( !$sku->product->on_sale ){
                        return $fail( '该商品未上架' );
                    }
                    if( $sku->stock === 0 ){
                        return $fail( '该商品已售完' );
                    }
                    preg_match( '/items\.(\d+)\.sku_id/' , $attribute , $m );
                    $index=$m[ 1 ];
                    // 根据索引找到用户所提交的购买数量
                    $amount=$this->input( 'items' )[ $index ][ 'amount' ];
                    if( $amount > 0 && $amount > $sku->stock ){
                        return $fail( '该商品库存不足' );
                    }
                } ,
            ] ,
            'items.*.amount'=>[ 'required' , 'integer' , 'min:1' ] ,
        ];
    }
}
